package cn.jlearn.proxy.web.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * Description: 服务调用结果集
 *
 * @param <T> 消息结果内容
 * @author qianyingchao
 * @version 1.0
 * @date 2021/1/6 3:43 下午
 */
public class R<T> implements Serializable {

    private static final long serialVersionUID = -1L;

    /**
     * 响应成功-code
     */
    private final static String RESP_CODE_SUCCESS = "200";

    /**
     * 响应成功-message
     */
    private final static String RESP_MESG_SUCCESS = "success";

    /**
     * 编码
     */
    @Getter
    @Setter
    private String code;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 消息
     */
    @Getter
    @Setter
    private String message;

    /**
     * 返回结果
     */
    @Getter
    @Setter
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;

    /**
     * 构造返回成功对象结果
     *
     * @return result
     */
    public static <T> R<T> success() {
        R<T> result = new R<>();
        result.setCode(RESP_CODE_SUCCESS);
        result.setMessage(RESP_MESG_SUCCESS);
        result.setSuccess(true);
        return result;
    }

    /**
     * 构造返回成功对象结果
     *
     * @param data 结果参数
     * @return result
     */
    public static <T> R<T> success(T data) {
        R<T> result = new R<>();
        result.setCode(RESP_CODE_SUCCESS);
        result.setMessage(RESP_MESG_SUCCESS);
        result.setSuccess(true);
        result.setData(data);
        return result;
    }

    /**
     * 构造失败对象
     *
     * @param code    编码
     * @param message 消息
     * @return result
     */
    public static <T> R<T> fail(String code, String message, T data) {
        R<T> result = new R<>();
        result.setCode(code);
        result.setData(data);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    /**
     * 构造失败对象
     *
     * @param code    编码
     * @param message 消息
     * @return result
     */
    public static <T> R<T> fail(String code, String message) {
        R<T> result = new R<>();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

}
